﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Ket_noi_toi_VPP_bang_mo_hinh_ba_lop.DAL;

namespace Ket_noi_toi_VPP_bang_mo_hinh_ba_lop
{
    public partial class FormSanPham : Form
    {
        SanPham SP = new SanPham();
        public FormSanPham()
        {
            InitializeComponent();
        }

        private void FormSanPham_Load(object sender, EventArgs e)
        {
            dtgHienThi.DataSource = SP.SelectAll();
        }

        private void btReset_Click(object sender, EventArgs e)
        {
            this.tbMaSP.Clear();
            this.tbTenSP.Clear();
            this.tbDonGia.Clear();
            this.tbSoLuong.Clear();
            this.tbMaNCC.Clear();
            this.tbMaNhom.Clear();
            this.tbMaSP.Focus();
        }

        private void btInsert_Click(object sender, EventArgs e)
        {
            SP.Insert(tbMaNhom.Text, tbMaNCC.Text, tbTenSP.Text, Convert.ToInt32(tbSoLuong.Text), Convert.ToInt32(tbDonGia.Text));
            FormSanPham_Load(sender,e);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            SP.Delete(Convert.ToInt32(tbMaSP.Text));
            dtgHienThi.DataSource = SP.SelectAll();
        }

        private void btSelect_Click(object sender, EventArgs e)
        {
            dtgHienThi.DataSource = SP.SelectID(Convert.ToInt32(tbMaSP.Text));
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            SP.Update(Convert.ToInt32(tbMaSP.Text), tbMaNhom.Text, tbMaNCC.Text, tbTenSP.Text, Convert.ToInt32(tbSoLuong.Text), Convert.ToInt32(tbDonGia.Text));
            dtgHienThi.DataSource = SP.SelectAll();
            FormSanPham_Load(sender,e);
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dtgHienThi_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            string ID;
            int dong;
            dong = e.RowIndex;
            ID = dtgHienThi.Rows[dong].Cells[2].Value.ToString();
            tbMaSP.Text = dtgHienThi.Rows[dong].Cells[0].Value.ToString();
            tbMaNhom.Text = dtgHienThi.Rows[dong].Cells[1].Value.ToString();
            tbMaNCC.Text = dtgHienThi.Rows[dong].Cells[2].Value.ToString();
            tbTenSP.Text = dtgHienThi.Rows[dong].Cells[3].Value.ToString();
            tbSoLuong.Text = dtgHienThi.Rows[dong].Cells[4].Value.ToString();
            tbDonGia.Text = dtgHienThi.Rows[dong].Cells[5].Value.ToString();            
        }

        private void btGoBack_Click(object sender, EventArgs e)
        {
            FormSanPham_Load(sender,e);
        }
    }
}
